task STEP1_BowTie2 {
  String refSuffix
  String sampleID
  String seroType
  String readNum
  String BaseDIR
  String filePath

  command {
	/opt/bowtie2-2.3.5.1-linux-x86_64/bowtie2 -x ${BaseDIR}/FMDV_reference/${refSuffix} -U ${filePath}/${sampleID}_R1_${seroType}_${readNum}.fastq -S paired.sam 
  }

  runtime {
    docker: "anneng01:8090/angs/pre_bowtie2:1.0.0"
  }

  output {
    File result = "paired.sam"
  }
}

task STEP2_SamTools {
  String InFile
  String BaseDIR
  String refSuffix
  command {
	samtools view -bS ${InFile} | samtools sort -o paired.sorted.bam
	samtools mpileup -Ou -f ${BaseDIR}/FMDV_reference/${refSuffix}.fna paired.sorted.bam -o mpileup.vcf
  }

  runtime {
    docker: "anneng01:8090/angs/pre_samtools:1.0.0"
  }
  
  output {
    File result = "mpileup.vcf"
  }
}

task STEP3_BcfTools {
  String InFile
  command {
	 
	 bcftools call -c ${InFile} | vcfutils.pl vcf2fq > SAMPLE_cns.fastq 
  }

  runtime {
    docker: "anneng01:8090/angs/pre_bcftools:1.0.0"
  }
  
  output {
    File result = "SAMPLE_cns.fastq"
  }
}


task STEP4_SeqTK {
  String InFile
  String sampleID
  String seroType
  command {
	seqtk seq -aQ64 -q20 -n N ${InFile} > ${sampleID}_${seroType}_cns.fasta
  }

  runtime {
    docker: "anneng01:8090/angs/pre_seqtk:1.0.0"
  }
  
  output {
    File result = "${sampleID}_${seroType}_cns.fasta"
  }
}



task ConcatFa {
  Array[String] files
  command {
	cat  ${sep=' '  files} > all.fa
  }

  runtime {
    docker: "anneng01:8090/angs/pre_seqtk:1.0.0"
  }
  
  output {
    File result = "all.fa"
  }
}


workflow wf {
   	String BaseDIR="/cromwell-executions/data/database"
	String filePath = "/data_raid1/bioinfo/app/cromwell/cromwell-executions/mngs_version_002/304148d3-9ae9-4bc1-8300-65bcd0df11c3/call-ExtractKUR_3/execution/result"
	Pair[String, Array[Object]] pair1=("Asia1",[{"sampleId":"SRR3664239","readNum":"153","refSuffix":"GCA_008797915.1_ASM879791v1_genomic"}])
	scatter(arr in pair1.right) {
		call STEP1_BowTie2 as s1 {
		  input:
		   refSuffix = arr.refSuffix,
  		   sampleID = arr.sampleId,
           seroType = pair1.left,
           readNum = arr.readNum,
           BaseDIR = BaseDIR,
           filePath = filePath
		}

		call STEP2_SamTools as s2 {
		  input:
		    refSuffix = arr.refSuffix,
		    BaseDIR=BaseDIR,
		    InFile=s1.result,
		}

		call STEP3_BcfTools as s3 {
		  input:
		    InFile=s2.result,
		}
		call STEP4_SeqTK as s4 {
	       input:
	         InFile = s3.result,
	         sampleID=arr.sampleId,
	         seroType=pair1.left

		}
	}

	call ConcatFa {
	  input:
        files=s4.result
	}
}




